package com.sms.fileupload;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sms.DB.ConnectDB;
import com.sms.DB.OperateDB;

public class NewProject {
	
	
	// 添加新的项目时进行数据的插入，成功返回true
	public boolean insertData(String fromID, String level, String proName, String deadline,
			String leader, List<String> member, String proDesc,
			List<Map> appendFile) {
		String sql = "";
		System.out.print(member.toString());

		boolean isSuccess = true;

		Connection conn = ConnectDB.getConnection();
		boolean autoCommit;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交

			// 向taskinfo表插入数据
			sql = "insert into taskinfo values(null, ?, ?, '项目任务', ?, ?, ?, now(), ?, '0', '0')";
			List<Object> tlist = new ArrayList<Object>();
			tlist.add(level);
			tlist.add(proName);
			tlist.add(proDesc);
			tlist.add(fromID);
			tlist.add(leader);
			tlist.add(deadline);
			OperateDB.setList(tlist);
			int tn = OperateDB.getGeneratedKey(sql);

			// System.out.println(tn);

			if (tn == 0) { // 插入taskinfo不成功
				isSuccess = false;
			} else {
				// 向groupinfo表插入数据
				sql = "insert into groupinfo values(null, ?, ?, '项目组')";
				List<Object> glist = new ArrayList<Object>();
				glist.add(leader);
				glist.add(tn);
				OperateDB.setList(glist);
				int gn = OperateDB.getGeneratedKey(sql);

				// System.out.println(gn);

				if (gn == 0) { // 插入groupinfo不成功
					isSuccess = false;
				} else {
					// 向groupmember插入数据
					sql = "insert into groupmember values(?, ?)";
					List<Object> gllist = new ArrayList<Object>();
					gllist.add(gn);
					gllist.add(leader);
					OperateDB.setList(gllist);
					if (!OperateDB.insert(sql)) {
						isSuccess = false;
					}
					
					if(isSuccess && member.size() != 0) {
							for (int i = 0; i < member.size(); i++) {
								String memberID = member.get(i);
								if (!memberID.equals(leader)) {
									sql = "insert into groupmember values(?, ?)";
									List<Object> gmlist = new ArrayList<Object>();
									gmlist.add(gn);
									gmlist.add(memberID);
									OperateDB.setList(gmlist);
									if (!OperateDB.insert(sql)) {
										isSuccess = false;
										break;
									}
								}
							}
					}

					if (isSuccess) { // 插入groupmember成功
						if (appendFile == null || appendFile.size() < 1) {// 没有添加附件时
							//System.out.println("没有附件");
							isSuccess = true;
						} else {// 有附件
							for (int i = 0; i < appendFile.size(); i++) {
								Map<String, String> map = new HashMap<String, String>();
								map = (Map<String, String>) appendFile.get(i);
								String fileName = map.get("fileName")
										.toString();
								String saveName = map.get("saveName")
										.toString();
								// 向appendinfo表插入数据
								sql = "insert into appendinfo values('项目任务', ?, ?, ?)";
								List<Object> alist = new ArrayList<Object>();
								alist.add(tn);
								alist.add(fileName);
								alist.add(saveName);

								OperateDB.setList(alist);
								if (OperateDB.insert(sql)) {// 插入附件表是否成功
									sql = "update taskinfo set isappend = '1' where taskid = "
											+ tn;
									if (OperateDB.update(sql) > 0) {
										isSuccess = true;
									} else {
										isSuccess = false;
									}
								} else {
									isSuccess = false;
								}
							}// for结束
						}
					}

				}

			}

			if (isSuccess) {
				conn.commit();// 手动提交
			} else {
				conn.rollback();
			}
			conn.setAutoCommit(autoCommit);// 恢复现场
		} catch (SQLException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			OperateDB.close(conn);
		}

		return isSuccess;
	}
}
